package com.neuedu.javase.jdbc.dao;

import com.neuedu.javase.jdbc.entity.User;
import org.junit.Test;

import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @auth 金山老师
 * title:   书籍代码
 * creatime: 2022-10-11 10:52
 * 描述 :
 */
public class UserDaoTest {

    @Test
    public void testInsert() {
        UserDao userDao = new UserDao();
        HashMap map = new HashMap();
        map.put("username1", "abc");
        map.put("displayname1", "abc昵称");
        map.put("password11", "987654321");
        map.put("birthday", "1980-01-02");
        map.put("gender", "1");
        map.put("telephone", "18888888888");
        map.put("email", "a@mail.com");
        int count = userDao.insert(map);
        System.out.println("count = " + count);
    }

    @Test
    public void testInsertByEntity() {

        for (int i = 0; i < 30; i++) {
            UserDao userDao = new UserDao();
            User user = new User();
            user.setUsername(i+"page");
            user.setDisplayname(i+":超级管理员");
            user.setPassword("987654321");
            user.setBirthday(new Date());
            user.setGender("2");
            user.setTelephone("18888888888");
            user.setEmail("b@mail.com");
            int count = userDao.insert(user);
            System.out.println("count = " + count);
        }


    }

    @Test
    public void deleteById() {
        UserDao userDao = new UserDao();

//        int count = userDao.deleteById(4);



        int id = 4;
        int count = userDao.deleteById(id);


        System.out.println("删除的条数 count = " + count);
    }

    @Test
    public void updateById() {
        UserDao userDao = new UserDao();

        //查询出来
        User user = new User(3,
                "abc-update",
                "新昵称",
                "@#$%^&*",
                new Date(),
                "1",
                "15888888888",
                "c@mail.com",
                new Date() ,
                null ,
                "0" );

        int count = userDao.updateById(user);


        System.out.println("更新的条数 count = " + count);

    }

    @Test
    public void selectById() {
        Integer id = 3;
        UserDao userDao = new UserDao();
        User user = userDao.selectById(id);
        System.out.println("user = " + user);
    }

    @Test
    public void selectList() {
        UserDao userDao = new UserDao();
        String condation = " where displayname like '%管理员%' ";
//        String condation = null;


        long total = userDao.selectCount(condation);
//        List<User> list = userDao.selectList(condation);
        int current  = 2;
        int pageSize = 10;
        List<User> list = userDao.selectList(condation,current,pageSize);
        System.out.println("total = " + total);

        for (User user : list) {
            System.out.println("user = " + user);
        }
    }


    @Test
    public void selectListByCondation() {
        UserDao userDao = new UserDao();
//        String condation = " where displayname like '%管理员%' ";
//        String username = "admin"; //模拟页面输入的信息
        //SQL ASP
//        String username = "admin' password = '12313'  or  displayname like '%%"; //模拟页面输入的信息
//        String username = "admin' password = '12313'  or  1=1 "; //模拟页面输入的信息
        String username = "admin"; //模拟页面输入的信息

//        String condation = " where username  = '"+username+"' ";
        String condation = " where username  = ?  ";


        List<User> list = userDao.selectList(condation , username);
        for (User user : list) {
            System.out.println("user = " + user);
        }

    }
}


















